package com.example.wanglv;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.wanglv.fragment.ListFragment;
import com.example.wanglv.fragment.MyFargment3;
import com.example.wanglv.fragment.MyFargment4;
import com.example.wanglv.fragment.MyFragment1;
import com.example.wanglv.fragment.MyFragment5;

import java.util.ArrayList;
import java.util.List;

public class Main2Activity extends AppCompatActivity implements ViewPager.OnPageChangeListener {
    RadioGroup rg;
    RadioButton rb0, rb1, rb2, rb3, rb4;
    ViewPager pager;
    MyFragment1 myFragment1;
    MyFargment3 myFargment3;
    MyFargment4 myFargment4;
    ListFragment listFragment;
    MyFragment5 myFragment5;
    FragmentManager fm;
    FragmentTransaction ft;
    MyAdpater myAdpater;
    TextView titl;
    List<Fragment> list;
    String[] tit = new String[]{"首页", "分类", "发现", "动态", "我的"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        initview();
    }

    void initview() {
        rg = findViewById(R.id.rg);
        pager = findViewById(R.id.viewpager);
        rb0 = findViewById(R.id.rb111);
        rb1 = findViewById(R.id.rb222);
        rb2 = findViewById(R.id.rb333);
        rb3 = findViewById(R.id.rb444);
        rb4 = findViewById(R.id.rb555);
        titl = findViewById(R.id.tv_titles);
        fragview();
        pager.addOnPageChangeListener(this);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb111:
                        pager.setCurrentItem(0);
                        break;
                    case R.id.rb222:
                        pager.setCurrentItem(1);
                        break;
                    case R.id.rb333:
                        pager.setCurrentItem(2);
                        break;
                    case R.id.rb444:
                        pager.setCurrentItem(3);
                        break;
                    case R.id.rb555:
                        pager.setCurrentItem(4);
                        break;
                }

            }
        });
    }

    void fragview() {
        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        list = new ArrayList<>();
        myFragment1 = new MyFragment1();

        myFargment3 = new MyFargment3();
        myFargment4 = new MyFargment4();
        listFragment = new ListFragment();
        myFragment5 = new MyFragment5();
        list.add(listFragment);
        list.add(myFragment1);
        list.add(myFargment3);
        list.add(myFargment4);
        list.add(myFragment5);
        myAdpater = new MyAdpater(fm, 0, list);
        pager.setCurrentItem(0);
        pager.setAdapter(myAdpater);
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        String t = tit[position];
        titl.setText(t);
        switch (position) {
            case 0:
                rb0.setChecked(true);
                break;
            case 1:
                rb1.setChecked(true);
                break;
            case 2:
                rb2.setChecked(true);
                break;
            case 3:
                rb3.setChecked(true);
                break;
            case 4:
                rb4.setChecked(true);
                break;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }


    public class MyAdpater extends FragmentPagerAdapter {
        List<Fragment> list;

        public MyAdpater(@NonNull FragmentManager fm, int behavior, List<Fragment> list) {
            super(fm, behavior);
            this.list = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list != null ? list.size() : 0;
        }
    }
}


